/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.talbd.proventas;

import edu.upn.talbd.proventas.dal.ClienteDAO;
import edu.upn.talbd.proventas.dal.CompraDAO;
import edu.upn.talbd.proventas.dal.EmpleadoDAO;
import edu.upn.talbd.proventas.dal.MejorClienteDAO;
import edu.upn.talbd.proventas.dal.MejorEmpleadoDAO;
import edu.upn.talbd.proventas.dal.ProveedorDAO;
import edu.upn.talbd.proventas.dal.VentaDAO;
import edu.upn.talbd.proventas.dal.VentasPorDiaDAO;
import edu.upn.talbd.proventas.entidades.Cliente;
import edu.upn.talbd.proventas.entidades.Compra;
import edu.upn.talbd.proventas.entidades.Empleado;
import edu.upn.talbd.proventas.entidades.MejorCliente;
import edu.upn.talbd.proventas.entidades.MejorEmpleado;
import edu.upn.talbd.proventas.entidades.Proveedor;
import edu.upn.talbd.proventas.entidades.Venta;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Administrador
 */
public class Informes extends javax.swing.JInternalFrame {

    /**
     * Creates new form Informes
     */
    private static int idVenta;
    private static int idCompra;

    public Informes() {
        initComponents();
        //Ventas
        ArrayList<Venta> colVenta = new ArrayList<Venta>();
        VentaDAO objVentaDAO = new VentaDAO();
        colVenta = objVentaDAO.buscar();
        DefaultTableModel temp = (DefaultTableModel) tblVentas.getModel();
        temp.setRowCount(0);
        
        ArrayList<Cliente> colCliente = new ArrayList<Cliente>();
        ClienteDAO objClienteDAO = new ClienteDAO();
        colCliente = objClienteDAO.buscar();
        
        int band = 0;
        double ventaTotal = 0;
        for(Venta v : colVenta){
            band=0;
            for(Cliente c : colCliente){
                if(v.getIdCliente() == c.getId()){
                    //System.out.println(“Nombre del Empleado: ” + tmpObjeto.getNombre());
                    Object nuevo[] ={v.getId(), v.getNumero(), v.getMontoTotal(),v.getIgvTotal(),v.getFecha(), c.getNombre()};
                    temp.addRow(nuevo);
                    ventaTotal = ventaTotal + Double.parseDouble(v.getMontoTotal().toString());
                    band=1;
                }
            }
            if(band==0){
                Object nuevo[] ={v.getId(), v.getNumero(), v.getMontoTotal(),v.getIgvTotal(),v.getFecha(), ""};
                temp.addRow(nuevo);
                ventaTotal = ventaTotal + Double.parseDouble(v.getMontoTotal().toString());
            }
        }
        
        txtVentaTotal.setText(Double.toString(ventaTotal));
        
        //Compras
        ArrayList<Compra> colCompra = new ArrayList<Compra>();
        CompraDAO objCompraDAO = new CompraDAO();
        colCompra = objCompraDAO.buscar();
        DefaultTableModel temp1 = (DefaultTableModel) tblCompras.getModel();
        temp1.setRowCount(0);
        
        ArrayList<Proveedor> colProveedor = new ArrayList<Proveedor>();
        ProveedorDAO objProveedorDAO = new ProveedorDAO();
        colProveedor = objProveedorDAO.buscar();
        double compraTotal = 0;
        int bandP = 0;
        for(Compra c : colCompra){
            bandP=0;
            for(Proveedor p : colProveedor){
                if(c.getIdProveedor() == p.getId()){
                    //System.out.println(“Nombre del Empleado: ” + tmpObjeto.getNombre());
                    Object nuevo[] ={c.getId(), c.getFechaCompra(), c.getMontoTotal(), p.getRazonsocial()};
                    temp1.addRow(nuevo);
                    compraTotal = compraTotal + Double.parseDouble(c.getMontoTotal().toString());
                    bandP=1;
                }
            }
            if(bandP==0){
                Object nuevo[] ={c.getId(), c.getFechaCompra(), c.getMontoTotal(), ""};
                temp1.addRow(nuevo);
                compraTotal = compraTotal + Double.parseDouble(c.getMontoTotal().toString());
            }
        }
        
        txtCompraTotal.setText(Double.toString(compraTotal));
        
        //Ventas por dia
        ArrayList<Venta> colVentaDia = new ArrayList<Venta>();
        VentasPorDiaDAO objVentasPorDiaDAO = new VentasPorDiaDAO();
        colVentaDia = objVentasPorDiaDAO.buscar();
        DefaultTableModel temp2 = (DefaultTableModel) tblVentasPorDia.getModel();
        temp2.setRowCount(0);
        
        for(Venta v : colVentaDia){
            Object nuevo[] ={v.getFecha(),v.getNumero(), v.getMontoTotal()};
            temp2.addRow(nuevo);
        }
        
        //Mejores Clientes
        ArrayList<MejorCliente> colMejorCliente = new ArrayList<MejorCliente>();
        MejorClienteDAO objMejorClienteDAO = new MejorClienteDAO();
        colMejorCliente = objMejorClienteDAO.buscar();
        DefaultTableModel temp3 = (DefaultTableModel) tblMejorCliente.getModel();
        temp3.setRowCount(0);
        
        colCliente = objClienteDAO.buscar();
        
        for(MejorCliente mc : colMejorCliente){
            for(Cliente c : colCliente){
                if(c.getId() == mc.getId()){
                    Object nuevo[] ={mc.getId(), c.getNombre(), mc.getNroCompras(), mc.getMonto()};
                    temp3.addRow(nuevo);
                }
            }
        }
        
        //Mejores Empleados
        ArrayList<MejorEmpleado> colMejorEmpleado = new ArrayList<MejorEmpleado>();
        MejorEmpleadoDAO objMejorEmpleadoDAO = new MejorEmpleadoDAO();
        colMejorEmpleado = objMejorEmpleadoDAO.buscar();
        DefaultTableModel temp4 = (DefaultTableModel) tblMejorEmpleado.getModel();
        temp4.setRowCount(0);
        
        ArrayList<Empleado> colEmpleado = new ArrayList<Empleado>();
        EmpleadoDAO objEmpleadoDAO = new EmpleadoDAO();
        colEmpleado = objEmpleadoDAO.buscar();
        
        for(MejorEmpleado me : colMejorEmpleado){
            for(Empleado e : colEmpleado){
                if(e.getId() == me.getId()){
                    Object nuevo[] ={me.getId(), e.getNombre(), me.getNroVentas(), me.getMonto()};
                    temp4.addRow(nuevo);
                }
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblVentas = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtVentaTotal = new javax.swing.JTextField();
        btnDetalle = new javax.swing.JToggleButton();
        lblRestriccionSeleccionV = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblCompras = new javax.swing.JTable();
        btnDetalleCompra = new javax.swing.JToggleButton();
        jLabel3 = new javax.swing.JLabel();
        txtCompraTotal = new javax.swing.JTextField();
        lblRestriccionSeleccionC = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblVentasPorDia = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblMejorCliente = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tblMejorEmpleado = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Informes");

        tblVentas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Numero", "Monto", "IGV", "Fecha", "Cliente"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblVentas);

        jLabel2.setText("TOTAL:");

        txtVentaTotal.setEditable(false);

        btnDetalle.setText("Detalle");
        btnDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleActionPerformed(evt);
            }
        });

        lblRestriccionSeleccionV.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 856, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnDetalle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblRestriccionSeleccionV)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 611, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtVentaTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtVentaTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDetalle)
                    .addComponent(lblRestriccionSeleccionV))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Ventas", jPanel1);

        tblCompras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Fecha", "Monto", "Proveedor"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblCompras);

        btnDetalleCompra.setText("Detalle");
        btnDetalleCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleCompraActionPerformed(evt);
            }
        });

        jLabel3.setText("TOTAL:");

        txtCompraTotal.setEditable(false);

        lblRestriccionSeleccionC.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 856, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(btnDetalleCompra)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblRestriccionSeleccionC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 611, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtCompraTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCompraTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDetalleCompra)
                    .addComponent(lblRestriccionSeleccionC))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Compras", jPanel2);

        tblVentasPorDia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Fecha", "Dia", "Total Vendido"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tblVentasPorDia);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 856, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Ventas por dia", jPanel3);

        tblMejorCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Cliente", "Nro. de Compras", "Monto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(tblMejorCliente);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 856, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Mejor cliente", jPanel4);

        tblMejorEmpleado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Empleado", "Nro. de Ventas", "Monto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane5.setViewportView(tblMejorEmpleado);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 856, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Mejor Empleado", jPanel5);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Seleccione Informe:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 744, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleActionPerformed
        // TODO add your handling code here:
        TableModel tableModel = tblVentas.getModel();
        int fila = tblVentas.getSelectedRow();
        if(fila == -1){
            lblRestriccionSeleccionV.setText("*Seleccione");
        }
        else{
         lblRestriccionSeleccionV.setText("");
        setIdVenta(Integer.parseInt(tableModel.getValueAt(fila, 0).toString()));
        
        VerDetalleVenta ventana = new VerDetalleVenta(null, closable);
        ventana.setVisible(true);
        }
    }//GEN-LAST:event_btnDetalleActionPerformed

    private void btnDetalleCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleCompraActionPerformed
        // TODO add your handling code here:
        TableModel tableModel = tblCompras.getModel();
        int fila = tblCompras.getSelectedRow();
        if(fila == -1){
            lblRestriccionSeleccionC.setText("*Seleccione");
        }
        else{
         lblRestriccionSeleccionC.setText("");
        setIdCompra(Integer.parseInt(tableModel.getValueAt(fila, 0).toString()));
        
        VerDetalleCompra ventana = new VerDetalleCompra(null, closable);
        ventana.setVisible(true);
        }
    }//GEN-LAST:event_btnDetalleCompraActionPerformed

    
    /**
     * @return the idVenta
     */
    public static int getIdVenta() {
        return idVenta;
    }

    /**
     * @param aIdVenta the idVenta to set
     */
    public static void setIdVenta(int aIdVenta) {
        idVenta = aIdVenta;
    }
    
    /**
     * @return the idCompra
     */
    public static int getIdCompra() {
        return idCompra;
    }

    /**
     * @param aIdCompra the idCompra to set
     */
    public static void setIdCompra(int aIdCompra) {
        idCompra = aIdCompra;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnDetalle;
    private javax.swing.JToggleButton btnDetalleCompra;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblRestriccionSeleccionC;
    private javax.swing.JLabel lblRestriccionSeleccionV;
    private javax.swing.JTable tblCompras;
    private javax.swing.JTable tblMejorCliente;
    private javax.swing.JTable tblMejorEmpleado;
    private javax.swing.JTable tblVentas;
    private javax.swing.JTable tblVentasPorDia;
    private javax.swing.JTextField txtCompraTotal;
    private javax.swing.JTextField txtVentaTotal;
    // End of variables declaration//GEN-END:variables
}
